<?php
namespace app\shouyin\admin;
use app\system\admin\Admin;
use app\system\model\systemUser as systemUserModel;
use app\shouyin\model\Operation as OperationModell;
use think\Db;

class Operation extends Admin
{
    protected $hisiModel = 'Operation';//模型名称[通用添加、修改专用]
    protected $hisiTable = '';//表名称[通用添加、修改专用]
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名

    public function index()
    {
        if ($this->request->isAjax()) {
            $where      = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $sysUser = systemUserModel::column('id,nick');

            $where['shop_id'] = session('shop_id');

            $data['data'] = OperationModell::with(['hasShop','hasRoom','hasUser'])
                            ->where($where)
                            ->page($page)
                            ->limit($limit)
                            ->order('id desc')
                            ->select();
            foreach ($data['data'] as $key => $value) {
                $data['data'][$key]['adviser'] = $sysUser[$data['data'][$key]['adviser_id']];
                $data['data'][$key]['technician'] = Db::name('system_user')->where('id','in',$data['data'][$key]['technician_ids'])->column('nick');
                $data['data'][$key]['projects'] = Db::name('project')->where('id','in',$data['data'][$key]['project_ids'])->column('name');
                $data['data'][$key]['cashier'] = Db::name('system_user')->where('id','in',$data['data'][$key]['cashier_id'])->column('nick');
            }

            $data['count'] = OperationModell::where($where)->count('id');
            $data['code'] = 0;
            return json($data);

        }
        return $this->fetch();
    }
}
